<?php 
class GroupsController extends Controller
{	
	public function listAction()
	{
		$model = new GroupsModel();
		
		if ($do = trim($this->request->getPost('do', '')))
		{
			if (strcmp($do, 'apply') === 0)
			{
				$new_group_id = $this->request->getPost('new_group_id');
				if (is_array($new_group_id))
				{
					$new_group_name = $this->request->getPost('new_group_name');
					foreach ($new_group_id as $id)
						$model->insertByAttributes(array('group_name' => $new_group_name[$id]));
				}
				
				$group_id = $this->request->getPost('group_id');
				if (is_array($group_id))
				{
					$group_name = $this->request->getPost('group_name');
					foreach ($group_id as $id)
						$model->updateByPk($id, array('group_name' => $group_name[$id]));
				}
				
				$this->forward(array('m' => '已成功保存对用户组所作的修改'));
			}
			else if (strcmp($do, 'delete') === 0)
			{
				$group_id = $this->request->getPost('group_id');
				if (is_array($group_id))
				{
					foreach ($group_id as $id)
						$model->deleteByPk($id);
				}
				
				$this->forward(array('m' => '已将' . count($group_id) . '个用户组彻底删除'));
			}
		}
		
		$this->view->groupsList = $model->findAllByCondition('group_id > ?', 1);
	}
}
